package com.qiantu.phone.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.ControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.activity.ControllerDeviceAcitvity;
import com.qiantu.phone.ui.activity.ControllerSettingActivity;
import com.qiantu.phone.ui.activity.ControllerTLPKGXActivity;
import com.qiantu.phone.ui.activity.LLRegionalGatewayActivity;

/**
 * 主机item
 */
public final class SmartControllerSubAdapter extends AppAdapter<ControllerBean> {

    private static final int TYPE_region = 0;
    private static final int TYPE_normal = 1;

    public SmartControllerSubAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_region) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_controller_sub, parent, false);
            return new RegionViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_controller, parent, false);
            return new NormalViewHolder(view);
        }

//        return new ViewHolder();
    }

    private final class NormalViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView iconController;
        private final TextView tvControllerName;
        private final TextView tvControllerId;
        private final ImageView iconControllerStatus;
        private final TextView tvControllerStatus;

        private NormalViewHolder(View contentView) {
            super(R.layout.item_controller_sub);
            iconController = findViewById(R.id.icon_controller);
            tvControllerName = findViewById(R.id.tv_controller_name);
            tvControllerId = findViewById(R.id.tv_controller_id);
            iconControllerStatus = findViewById(R.id.icon_controller_status);
            tvControllerStatus = findViewById(R.id.tv_controller_status);
            itemView.setOnClickListener(mOnClickListener);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindView(int position) {
            //在线状态：离线 = 0,在线 = 1,未知 = 2,网络异常 = 3
            ControllerBean item = getItem(position);
            itemView.setTag(item);
            if (item.isIsDisable()) {
                tvControllerStatus.setVisibility(View.INVISIBLE);
                iconControllerStatus.setVisibility(View.INVISIBLE);
            } else {
                iconControllerStatus.setVisibility(View.VISIBLE);
                switch (item.getStateType()) {
                    case 0:
                        tvControllerStatus.setText(R.string.offline);
                        tvControllerStatus.setEnabled(false);
                        tvControllerStatus.setVisibility(View.VISIBLE);
                        iconControllerStatus.setEnabled(false);
                        break;
                    case 1:
                        tvControllerStatus.setText(R.string.normal);
                        tvControllerStatus.setEnabled(true);
                        iconControllerStatus.setEnabled(true);
                        tvControllerStatus.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        tvControllerStatus.setText(R.string.fault);
                        tvControllerStatus.setEnabled(false);
                        iconControllerStatus.setEnabled(false);
                        tvControllerStatus.setVisibility(View.VISIBLE);
                        break;
                }
            }
            GlideApp.with(iconController).load(item.getImageUrl()).into(iconController);
            tvControllerName.setText(item.getName());
            if (item.getSlaveArr() != null) {
                tvControllerId.setText(item.getCodeNo() + "\n" + item.getSlaveArr().size() + "个从机");

            } else {
                tvControllerId.setText(item.getCodeNo());
            }
        }
    }

    private final class RegionViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView iconController;
        private final TextView tvControllerName;
        private final TextView tvControllerId;
        private final TextView tvControllerStatus;
        private final TextView tvSlaveCount;


        private RegionViewHolder(View contentView) {
            super(R.layout.item_controller_sub_area);
            iconController = findViewById(R.id.icon_controller);
            tvControllerName = findViewById(R.id.tv_controller_name);
            tvControllerId = findViewById(R.id.tv_controller_id);
            tvControllerStatus = findViewById(R.id.tv_controller_status);
            tvSlaveCount = findViewById(R.id.tv_slave_count);
            itemView.setOnClickListener(mOnClickListener);

        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindView(int position) {
            //在线状态：离线 = 0,在线 = 1,未知 = 2,网络异常 = 3
            ControllerBean item = getItem(position);
            itemView.setTag(item);

            switch (item.getStateType()) {
                case 0:
                    tvControllerStatus.setText(R.string.offline);
                    tvControllerStatus.setEnabled(false);
                    tvControllerStatus.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    tvControllerStatus.setText(R.string.online);
                    tvControllerStatus.setEnabled(true);
                    tvControllerStatus.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    tvControllerStatus.setText(R.string.fault);
                    tvControllerStatus.setEnabled(false);
                    tvControllerStatus.setVisibility(View.VISIBLE);
                    break;
            }

            GlideApp.with(iconController).load(item.getImageUrl()).into(iconController);
            tvControllerName.setText(item.getName());
            tvControllerId.setText(item.getCodeNo());
            if (item.getSlaveArr() != null) {
                tvSlaveCount.setText(item.getSlaveArr().size() + "个从机");
            } else {
                tvSlaveCount.setText("");
            }
        }
    }


    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }

    @Override
    public int getItemViewType(int position) {

        ControllerBean item = getItem(position);
        if (item.getProductModel().equals("0x1503")) {
            return TYPE_region;
        } else {
            return TYPE_normal;
        }


//        return super.getItemViewType(position);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ControllerBean item = (ControllerBean) v.getTag();
            if (item.getProductModel().equals("0x1503")) {
                //区域网关
                LLRegionalGatewayActivity.start(getContext(), item);
            } else if (item.getProductModel().equals("0x1005") || item.getProductModel().equals("0x0509")) {
                ControllerSettingActivity.start(getContext(), item.getControllerSerialNo());
            } else if (item.getProductModel().equals("0x1006")) {
                ControllerTLPKGXActivity.start(getContext(), item.getControllerSerialNo());
            } else {
                ControllerDeviceAcitvity.start(getContext(), item.getControllerSerialNo().toString());
//                ControllerSettingActivity.start(getContext(), item.getControllerSerialNo().toString());
            }

        }
    };
}